package com.concurrent.threadpool.thread;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Date 2021/8/10 11:17
 * @Author WangLinBao
 * @Description
 */
public class TestThread {

    public static void main(String[] args) {
        int coreSize = 1;
        int maximumPoolSize = 10;
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue(5);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (; ; ) {
                    try {
                        System.out.println(Thread.currentThread().getName() + "========");
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        queue.add(runnable);
        queue.add(runnable);
        queue.add(runnable);
        queue.add(runnable);
        queue.add(runnable);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(coreSize,maximumPoolSize,
                3000, TimeUnit.SECONDS,queue,new MyThreadFactory());
        System.out.println("====");
        BlockingQueue<Runnable> queue1 = executor.getQueue();
        System.out.println("queue1 = " + queue1.size());
        System.out.println("queue1 = " + queue1.toString());
//        executor.execute(runnable);
//        System.out.println("executor.prestartAllCoreThreads() = " + executor.prestartAllCoreThreads());
//        for (Runnable runnable1 : queue1) {
//            System.out.println(runnable1.);
//        }
//        executor.execute(runnable);
//        queue.add(runnable);
//        try {
//            Thread.currentThread().join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }
    static class MyThreadFactory implements ThreadFactory {
        private static final AtomicInteger poolNumber = new AtomicInteger(1);
        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;

        MyThreadFactory() {
            SecurityManager s = System.getSecurityManager();
            group = (s != null) ? s.getThreadGroup() :
                    Thread.currentThread().getThreadGroup();
            namePrefix = "wlb-pool-" +
                    poolNumber.getAndIncrement() +
                    "-thread-";
        }

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r,
                    namePrefix + threadNumber.getAndIncrement(),
                    0);
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != Thread.NORM_PRIORITY)
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }
    }
}
